package com.dmcb.trade.business.utils;

import com.dmcb.common.business.utils.JsonUtil;
import com.dmcb.common.business.utils.WebClientUtil;
import com.gargoylesoftware.htmlunit.WebClient;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by miting on 2019/1/14
 */
public class FengHuangCrawlUtil {

    public static Map<String, String> getInfo(String link) {
        WebClientUtil webClientUtil = new WebClientUtil();
        WebClient webClient = webClientUtil.initWc(false, false, false);
        String data = webClientUtil.getDoc2(webClient,link).toString();
        Map<String, String> map = new HashMap<>();
        if (data.contains("allData = ")) {
            map = getMapData(data);
        }else if(data.contains("403 Forbidden")){
            return null;
        }
        return map;
    }

    private static Map<String, String> getMapData(String data) {

        Map<String, Object> map;
        data = data.substring(data.indexOf("allData = ") + 10);
        data = data.substring(0, data.indexOf("var adData"));
        if (!data.contains("docData")){
            return null;
        }
        data = data.substring(data.indexOf("\"docData\":") + 10, data.lastIndexOf("};"));
        map = JsonUtil.getResult(data);
        if (map == null || map.size() == 0) {
            return null;
        }
        //标题
        String title = (String) map.get("title");
        if (StringUtils.isBlank(title)) {
            return null;
        }
        Map<String, String> map1 = new HashMap<>();
        map1.put("title", title);
        return map1;
    }
}
